/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {
    var url = '/v1/manager/departments.json';
    var data = {
        search : '',
        page : 1,
        page_size : 10
    };
    getDatas(url, data);

    $(document).on('keyup', '.search-key', function(){
        data.search = $(this).val();
        data.page = 1;
        getDatas(url, data);

    });

    $(document).on('click', '.page-list a', function(){
        data.search = $('.input-sm').val();
        data.page = $(this).attr('data-id');
        getDatas(url, data);
    });

    $(document).on('click', '.btn-delete', function(){
        deleteData('/v1/manager/delete/department', $(this).attr('data-id'));

    });
});



function getDatas(url, data) {
    sendAjax(url, 'get', data, function (json) {
        console.log(json);
        var html = '';
        var page_html = '';
        if (json.body.rows.length > 0) {
            $.each(json.body.rows, function (n, v) {
                var color = 'bg-assistant-active';
                var star = '';
                var count = 0;
                if (v.score >= 90) {
                    count = 5;
                } else if (v.score >= 60) {
                    count = 3;
                } else {
                    count = 0;
                }
                for (var i = 0; i < count; i ++) {
                    star += '<i class="fa fa-star"></i>';
                }
                html += '<div class="col-lg-3 col-md-4 col-sm-6 cos-xs-12">';
                html += '<div class="box box-widget widget-user">';
                html += '<div class="widget-user-header '+color+'"></div>';
                html += '<div class="widget-user-image">';
                html += '<img class="img-circle" src="/img/department.png" alt="User Avatar"></div>';
                html += '<div class="box-body" style="margin-top: 30px;">';
                html += '<div class="row"><div class="col-sm-12"><div class="description-block">';
                html += '<h5 class="description-header">'+v.name+'</h5></div></div>';
                html += '</div>';
                html += '<div class="row"><div class="col-sm-4">';
                html += '<div class="description-block"><span class="description-text text-left">堡垒指数</span></div></div>';
                html += '<div class="col-sm-8"><div class="description-block">';
                html += '<span class="description-text">'+v.score+'分</span></div></div></div>';
                html += '<div class="row"><div class="col-sm-4">';
                html += '<div class="description-block"><span class="description-text text-left">星级</span></div></div>';
                html += '<div class="col-sm-8"><div class="description-block">';
                html += '<span class="description-text text-red">'+star+'</span></div></div></div></div>';
                html += '<div class="box-footer u-footer-back" style="padding-top: 0;">';
                html += '<div class="row"><div class="col-sm-6 border-right"><div class="description-block">';
                html += '<span class="description-text"><a href="/manager/department/detail-'+v.id+'.html" title="详情"><i class="fa fa-list-ul"></i></a></span></div></div>';
                html += '<div class="col-sm-6"><div class="description-block"><span class="description-text">';
                html += '<a class="btn-delete" href="javascript:;" data-id="'+v.id+'" title="删除"><i class="fa fa-trash"></i></a></span></div></div></div></div></div></div>';
            });
            page_html = pageList(data.page, data.page_size, json.body.total, json.body.pages, json.body.rows.length);
        }
        $('.data-list').html(html);
        $('.page-list').html(page_html);
    });
}
